#!/usr/bin/python3

import requests


'''Accept	text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding	gzip, deflate, br
Accept-Language	zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Connection	keep-alive
Host	kyfw.12306.cn
Upgrade-Insecure-Requests	1
User-Agent	Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'''


def query():
    print("query")
    url = "https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2020-01-07&leftTicketDTO.from_station=GZQ&leftTicketDTO.to_station=BJP&purpose_codes=ADULT"
    # url = "http://zjl.carfunny.com.cn/json/music.json"


    # 添加请求头
    headers = {
        'Host': 'kyfw.12306.cn',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0',
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': 'gzip, deflate, br',
        'If-Modified-Since': '0',
        'Cache-Control': 'no-cache',
        'X-Requested-With': 'XMLHttpRequest',
        'Connection': 'keep-alive',
        'Referer': 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc',
        'Cookie': 'JSESSIONID=A96FC5D3E2FC3E05246484900D783F67; route=c5c62a339e7744272a54643b3be5bf64; BIGipServerotn=2581004554.24610.0000; RAIL_EXPIRATION=1578322289588; RAIL_DEVICEID=OH8CZA02ZKxVsuoW48kBfePZTCC8ToENyyXjrLZvotFjbbqdZXByNmzz8rF40yTJ7s4vDTq127DI3PDfUDdxy5TfbnPDWrwbJ-P3sHrdTYMyI_TaVabzt90YgDFr5An6GyE3pnRSA2ocwYHDY6bmQe4JWMhqsCLw; _jc_save_fromStation=%u5E7F%u5DDE%2CGZQ; _jc_save_toStation=%u5317%u4EAC%2CBJP; _jc_save_fromDate=2020-01-07; _jc_save_toDate=2020-01-03; _jc_save_wfdc_flag=dc'

    }
    response = requests.get(url, headers=headers)
    print(response.text)
    jsonData = response.json()
    # dict = jsonData['data']['map']
    # jsonData['data']['result']
    return jsonData['data']


queryResult = query()

dict = queryResult['map']
print("map:")
print(dict)

print("\n\n")
print("车次\t出发站\t到达站\t出发时间\t到达时间\t历时\t软卧一待卧")
for i in queryResult['result']:
    temp_data = i.split("|")
    cheCi = temp_data[3]
    chufa = dict[temp_data[6]]
    daoda = dict[temp_data[7]]
    start = temp_data[8]
    end = temp_data[9]
    lishi = temp_data[10]
    ruanwo = temp_data[23]

    # print(temp_data)
    if len(cheCi) < 4:
        cheCi = cheCi + "\t"
        # print("len:" + str(len(cheCi)))
    print(cheCi + "\t" + chufa + "\t" + daoda + "\t" + start + "\t\t" + end + "\t\t" + lishi + "\t" + ruanwo)

